// 获取url参数
var QueryString = {
    data: {},
    Initial: function() {
        var aPairs, aTmp;
        var queryString = new String(window.location.search);
        queryString = queryString.substr(1, queryString.length);
        aPairs = queryString.split("&");
        for (var i = 0; i < aPairs.length; i++) {
            aTmp = aPairs[i].split("=");
            this.data[aTmp[0]] = aTmp[1];
        }
    },
    GetValue: function(key) {
        return this.data[key];
    }
}



$(document).ready(function() {
    // 取得json地址
    var jsonUrl = getJsonUrl();

    // 取公告新闻json文件
    $.get(jsonUrl, function(nData) {
//        console.dir(nData);
        nData = JSON.parse(nData)
        var htmlStr = '';
        htmlStr = createHtml(nData);
        $("#content").html(htmlStr);
    });
});

// 拼接json 文件地址
function getJsonUrl(jsonName) {
    QueryString.Initial();
    var jsonName = QueryString.GetValue("jName");
    var jsonUrl = '../js/data/' + jsonName + '.json';
    return jsonUrl;
}

// 创建新闻公告的html
function createHtml(nData) {
    var htmlStr = '';
    for (var i = 0, j = nData.length; i < j; i++) {
        if (nData[i].level == "title") {
            var titleHtml = createTitle(nData[i]);
            htmlStr += titleHtml;
        } else if (nData[i].level == "source") {
            htmlStr += createSource(nData[i]);
        } else if (nData[i].level == "phead") {
            htmlStr += createPheader(nData[i]);
        } else if (nData[i].level == "pcontent") {
            htmlStr += createPhtml(nData[i]);
        }else if(nData[i].level == "story"){
            htmlStr += createStory(nData[i]);
        }
    }

    return htmlStr;
}

// 创建标题
function createTitle(tData) {
    var tHtmlStr = '<h1';
    tHtmlStr = addClass(tData, tHtmlStr);
    tHtmlStr += '>';
    tHtmlStr += tData.content + '</h1>';
    return tHtmlStr;
}

function createSource(sData) {
    var sHtmlStr = '<div'
    sHtmlStr = addClass(sData, sHtmlStr);
    sHtmlStr += '>';
    sHtmlStr += '<span class="pub_date">发布时间:' + sData.time + '</span>' +
        '<span id="media_name">来源：' + sData.source + '</span>' ;
        if(sData.author){
           sHtmlStr += '<span id="pub_author">作者: ' + sData.author + '</span>';  
        }
        

    sHtmlStr += '</div>';

    return sHtmlStr;
}

// 创建段落标题
function createPheader(phdata) {
    var phHtmlStr = '<h3';
    phHtmlStr = addClass(phdata, phHtmlStr);
    phHtmlStr += '>';
    phHtmlStr += phdata.content + '</h3>';
    return phHtmlStr;
}

//  创建段落内容
function createPhtml(pdata) {
    var pHtmlStr = '<p';
    pHtmlStr = addClass(pdata, pHtmlStr);
    pHtmlStr += '>';
    if (pdata.level2) {

    } else {
        pHtmlStr += pdata.content;
    }
    pHtmlStr += '</p>';
    return pHtmlStr;
}

// 创建新闻公告里的故事
function createStory(sdata) {
    var sHtmlStr = '<div';
    sHtmlStr = addClass(sdata,sHtmlStr);
    sHtmlStr += '>';
    sHtmlStr +='<h2 class="story_title">' + sdata.stitle + '</h2>';
    sHtmlStr += createPhtml(sdata.scontent);
    sHtmlStr += '</div>';

    return sHtmlStr;
}
// 增加class
function addClass(hData, hStr) {
//    console.log("hData.class:" + hData.class);
    if (hData.className) {
        hStr += ' class="' + hData.className + '"';
    }

    return hStr;
}